/**
 *
 * @author ace
 * @create 2017/7/27
 */
class CircleMainItem extends ComponentEx {


    private lab_rank: eui.Label;
    private lab_roomid: eui.Label;
    private lab_fangka: eui.Label;
    //规则问号
    private btn_rule: ButtonEx;

    //创建规则和规则显示
    private btn_set_rule: ButtonEx;
    //private btn_mark: ButtonEx;

    private lab_roundtime: eui.Label;
    private cellGroup: eui.Group;
    private cell_1: CircleMainItemCell;
    private cell_2: CircleMainItemCell;
    private cell_3: CircleMainItemCell;
    private cell_4: CircleMainItemCell;

    private btnGroup: eui.Group;
    private btn_join: ButtonEx;
    private btn_details: ButtonEx;
    private btn_play: ButtonEx;
    private btn_share: ButtonEx;

    private btn_share_details: ButtonEx;

    private img_yikai: eui.Image;
    private lab_createtime: eui.Label;
    private lab_duiju: eui.Label;

    private tips: TextTips;

    constructor() {
        super();
        this.skinName = "CircleMainItemSkin";
    }

    childrenCreated() {
        super.childrenCreated();


        this.tips = new TextTips(this.btn_rule);

        this.btn_join.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_details.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_play.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_share.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_share_details.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_set_rule.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
    }

    private clickHandler(e: egret.TouchEvent) {
        if (!game.isBtnDisabledByTime) game.isBtnDisabledByTime = true;
        else return;
        if (!this.data) return;

        switch (e.currentTarget) {

            //加入点击
            case this.btn_join:
                game.roomid = Circle.args_2017.roomid ? Circle.args_2017.roomid : 0;
                Circle.circle_send(2017, Circle.args_2017);
                break;
            //详情点击
            case this.btn_details:

                game.roomid = this.data.roomid;
                game.recordInfos = this.data.persons;
                Replayer.shareIF.item_id = this.data.item_id.split("#")[1];
                gameManager.socketManager.send(20, { id: this.data.id });
                this.saveDetailsRoomData();
                break;
            case this.btn_play:
                game.roomid = game.circleRoomInfo.roomid;
                gameManager.socketManager.send(21, { id: this.data[1] });
                game.ReplayType = ReplayType.circle;
                break;
            case this.btn_share://分享点击
                Weixin.onCircleJoinShare();
                game.share.show();
                break;
            case this.btn_share_details:// 战绩分享
                var info = game.recordInfos
                for (let i: number = 0; i < info.length; i++) {
                    if (info[i].pos == 1) {
                        Replayer.shareIF.uid = info[i].uid;
                    }
                }
                Replayer.shareIF.playback_id = this.data[1].split("#")[1];
                Replayer.shareIF.share_desc = "来自: " + game.player.nick + "   时间: " + this.timeStr;
                Weixin.shareReplay();
                break;
            case this.btn_set_rule:
                var panel = gameManager.find("CircleMainPanelAI");
                if (panel) panel.hide();

                panel = gameManager.find("CircleCreatePanel");

                if (panel) panel.show();

                break;

        }

    }

    /**存本房间信息 */
    saveDetailsRoomData() {
        const info = new CircleRoomInfo();
        const data = this.data;
        info.cost = data.cost;
        info.roomid = data.roomid;
        info.timer = this.timeStr;
        info.rank = this.rank;
        info.id = this.data.id;
        game.circleRoomInfo = info;
    }

    update(state: string, data: any) {
        this.skinState = state;
        if (!data) return;
        this.data = data;
        switch (state) {
            case "current":
                this.refresh_current();
                break;
            case 'list':
                this.refresh_list();
                break;
            case 'history':
                this.refresh_history();
                break;
            case 'details':
                this.refresh_details();
                break;
        }
    }

    refresh_current() {

        let cell: CircleMainItemCell;
        var info: any;
        let state: string;
        this.btn_set_rule.visible = <boolean>Circle.circle_data.ifown;
        this.btn_rule.visible = !<boolean>Circle.circle_data.ifown;
        this.tips.desc = FashionTools.getDaikaiRoomDes(this.data);


        Circle.args_2017 = {
            id: Circle.args_2015.id,
            owner_uid: Circle.args_2015.owner_uid,
            uid: Circle.args_2015.uid,
            type: GameConfig.gameType,
            pass: "0"
        };

        if (this.data.roomid) Circle.args_2017.roomid = this.data.roomid;


        //判断是否有空闲房间
        if (this.data.info && this.data.info.length) {
            //this.lab_roomid.text = '房间:' + this.data.roomid;
            for (let i: number = 1; i <= 4; i++) {

                cell = this['cell_' + i];
                info = this.data.info[i - 1];
                state = !!info ? this.skinState : "empty";
                cell.update(state, this.data.info[i - 1], this.data.roomid);
                cell.showKill = Circle.circle_data.ifown && info;
                cell.lock(i > this.data.renshu);
            }
        } else {
            this.lab_roomid.text = '';
            for (let i: number = 1; i <= 4; i++) {
                cell = this['cell_' + i];
                cell.update("empty", null);
                cell.lock(i > this.data.renshu);
            }
        }
    }

    refresh_list() {
        this.lab_roomid.text = '' + this.data.roomid;
        this.lab_fangka.text = '' + this.data.cost + "张";
        // this.btn_rule.source = "btn_mark3";
        this.tips.desc = FashionTools.getDaikaiRoomDes(this.data);

        let cell: CircleMainItemCell;
        for (let i: number = 1; i <= 4; i++) {
            cell = this['cell_' + i];

            cell.update(this.skinState, this.data.info[i - 1], this.data.roomid);
            //cell.roomStatus = this.data.status;
            cell.lock(i > this.data.renshu);
        }
    }

    refresh_history() {
        this.lab_roomid.text = '' + this.data.roomid;
        this.lab_fangka.text = '' + this.data.cost + "张";
        this.lab_createtime.text = '' + this.timeStr;

        // this.btn_rule.source = "btn_mark3";
        this.tips.desc = FashionTools.getDaikaiRoomDes(this.data);

        let person: any;
        let cell: CircleMainItemCell;
        for (let i: number = 1; i <= 4; i++) {
            cell = this['cell_' + i];
            cell.lock(i > this.data.persons.length);
            person = this.data.persons[i - 1];
            if (!person) continue;
            person.cur = this.data.total[person.pos - 1];
            cell.update(this.skinState, person, this.data.roomid);
            cell.lock(i > this.data.persons.length);
        }
    }

    refresh_details() {
        this.lab_createtime.text = '' + this.timeStr;

        let person: any;
        let cell: CircleMainItemCell;
        for (let i: number = 1; i <= 4; i++) {
            cell = this['cell_' + i];
            person = game.recordInfos[i - 1];
            if (person) {
                cell.visible = true;
                person.cur = this.data[person.pos + 1];
                cell.update(this.skinState, person);
            }
            else {
                cell.lock(true);
            }
        }
    }
    private _rank: number;
    public set rank(va: number) {
        this._rank = va;
        if (this.skinState == 'details') {
            this.lab_rank.text = '第' + va + '把';
            this.lab_duiju.text = "第" + va + "局";
        }
        else {
            this.lab_rank.text = '' + va;
        }
    }

    public get rank(): number {
        return this._rank;
    }

    private get timeStr() {
        if (this.skinState == 'list') {
            return StringUtils.getYTDByTimestamp(this.data.create_time * 1000) + ' ' + StringUtils.getHMSByTimestamp(this.data.create_time * 1000);
        }
        else if (this.skinState == 'history') {
            return StringUtils.getYTDByTimestamp(this.data.time * 1000) + ' ' + StringUtils.getHMSByTimestamp(this.data.time * 1000);
        }
        else if (this.skinState == 'details') {
            return StringUtils.getYTDByTimestamp(this.data[0] * 1000) + " " + StringUtils.getHMSByTimestamp(this.data[0] * 1000);
        }
        return "null";
    }
}
class CircleRoomInfo {
    timer: string
    cost: number
    roomid: number
    rank: number
    id: string
}